Developing Chatbots with RASA- From Intuition to Implementation & Deployment
“To the user, chatbots seem to be “intelligent” due to their informative skills. However, chatbots are only as intelligent as the underlying database.”
With Advancement in Natural language Processing and various open-source frameworks available, various industries and organizations are trying to implement automated text reply as a response, or, if I have to put in simple words, developing a chatbot. But have you ever wondered, how would it feel like, if you have to make a basic chatbot within 20–30 mins easily!
In this article, I would like to cover basic intuitions; what is RASA, About its architecture, and how to implement or develop a chatbot using the RASA framework.
Contents:
- Chatbots — Introduction, and Applications
- RASA — Why Use it?
- RASA Model’s Architecture
- RASA’s Basic Aspects and Terminologies
- Installing Basic Dependencies and Creating Environments and Suitable Packages for working on Chatbots.
- Coding Implementation- Making A Basic FAQ based Chatbot Integrated with API Of COVID-19 Tracker’s Data To fetch the Number of cases from all states
- Testing Our Chatbot And Deployment
- Pros and Cons Of Using RASA
- Conclusion
- Source Code of this COVID-19 Chatbot Project
First, let us get to know what actually are chatbots?
A chatbot is an application that can initiate and continue a conversation using auditory and/or textual methods as a human would do. A chatbot can be either a simple rule-based engine or an intelligent application leveraging Natural Language Understanding. Many organizations today have started using chatbots extensively. Chatbots are becoming famous as they are available 24*7, provide consistent customer experience, can handle several customers at a time, are cost-effective and hence, results in better overall customer experience.
Uses :
· Customer support
· Frequently Asked Questions
· Addressing Grievances
· Appointment Booking
· Automation of routine tasks
· Address a query
So, What is RASA?
With a sophisticated NLU engine that can power contextual AI assistants over text or voice, Rasa is a chatbot framework to consider for more ambitious projects. Unlike many bot frameworks, Rasa is also open source. A developer community provides NLU training models, bot prototypes, and guidance. It’s incredibly powerful and is used by developers worldwide to create chatbots and contextual assistants. You can build, deploy, and host the implementation internally which makes the chatbot and the related data more secure. Further, it also gives you better control and flexibility in deploying your chatbot in production.
With help of RASA, you can easily develop a chatbot according to your needs — whether it is FAQ based, or appointment booking assistant, thanks to its easy to go to documentation and organized files layout, you can integrate or expose it to and other frameworks, API’s and links or databases of your own website to speed up your needs.
Basics Aspects of Chatbot Development:
- Intent Classification :
Intent classification is an important component of Natural Language Understanding (NLU) systems in any chatbot platform. When I say the word intent, I mean intention — what we are trying to imply as humans to the chatbot and the ability of the chatbot to understand what specific thing or “intention” we are trying to convey to our chatbot.
Basically, Intents are Verbs in a dialog. For Example, If a user says “I want to book a cab” or “I want to purchase a book” then the user’s intent is to Book a Cab and Purchase or Order a Book
2.Entity Classification:
Entity Classification or recognition is information extraction that seeks to locate and classify named entities in text into pre-defined categories such as the names of persons, organizations, locations, expressions of times, quantities, monetary values, percentages, etc.
Entities are basically Nouns in your Dialog
For Example -” I would like to book or reserve a Flight from Mumbai to Chennai for Tomorrow Night”. Here the Entities are :
- Time: Tomorrow Night
- Place: Mumbai, Chennai
Architecture or Layout of Rasa Framework:
Basically, Rasa has following inbuilt-modules, being used while implementing a chatbot:
1.RASA NLU for understanding user messages. This part of the framework is the tool/library for intent classification and entity extraction from the query text. The entities and intents further enable response retrieval and composition of the utterance text.
You can build simple/minimal AI chatbots by using just this component itself. This is usually the case while building an AI chatbot to respond to FAQs, simple retrieval queries, etc. In fact, in this blog, we will be using it to code our chatbot too.
2.RASA Core for holding conversations and deciding what to do next. This component is the dialogue engine for the framework and helps in building more complex AI assistants that are capable of handling context (previous queries and responses in the conversation) while responding. Though RASA recommends using both NLU and Core, they can be used independently of each other.
3.Deployment Channels and Integrations:
These components let you connect and deploy your bot on popular messaging platforms. You can read more about these here. These help the developers in focusing more on the bot functionalities and less on the plumbing required to deploy it in the real world. RASA X is a toolset that takes your bot (developed using RASA opensource) to the next level. This is a free but closed source toolset available to all developers.
Some popular choices of developers for deploying RASA chatbot are:
- Telegram
- Slack
- Your Own Website
- Facebook Messenger
- Twilio
- Google Hangouts Chat
- Microsoft Bot Framework
- Cisco Webex Teams
- RocketChat
- Mattermost
Let us try to understand the working of RASA chatbot :
The Following steps explained below:
- First, The message is received by the user into the bot and passed to an Interpreter, which converts it into a dictionary including the original text, the intent, and any entities that were found. This part is handled by NLU.
- Then, the Tracker is the object which keeps track of the conversation state. It receives the info that a new message has come in. It also helps to keep the track of entities extracted from the user and also how the conversation is managed,i.e., the dialog flow.
- The policy receives the current state of the tracker.
- The policy chooses which action to take next.
- The chosen action is logged by the tracker since it helps to keep the track of path or flow of the conversation.
- A response is sent to the user. The user then replies according to the response he/she gets from the bot.
Enough ChitChat, Let’s Get into The Coding Stuff!
Implementing Chatbot:
- Prerequisites
The prerequisites for developing and understanding a chatbot are:
- Python installed
- Microsoft Build tools with visual c++ 14.0 installed. Link: https://visualstudio.microsoft.com/downloads/
→ After Setting Up the Above Prerequisites:
- Create a new folder for your chatbot project.
- Open that folder using any IDE You want. I have used Pycharm, But you can use any other popular IDE like VSCode or Atom.
- It Is always a good practice to create a new environment for any of your python based project.
- I am going to do the same, Chatbot project from pycharm or from anaconda prompt. This will create an environment named rasa, with further supporting dependencies
conda create --name rasa- Then Activate This environment in anaconda prompt itself using:
activate rasaNOTE: You can also select an environment in IDE’s too. Pycharm and VSCode have dedicated interpreters and environment selecting options in Project settings itself.
- Run the command pip install rasa for installing all the rasa dependencies
- Run the command pip install spacy for installing the spacy library.
- Then enter the following commands:
python -m spacy download enpython -m spacy download en_core_web_mdpython -m spacy link en_core_web_md en
NOTE: Try installing these pip modules by running command prompt at administrator level [Run As Administrator]
- Then, initiate this project in IDE, make any empty python file, right-click on it and select, open in terminal. And in that terminal, make sure your project directory is showing up, this is obvious if you have created that python file in the project directory itself.
- In that terminal, type :
rasa init- Then, for all the subsequent actions choose Y (for selecting the directory of your project, for training the model etc).
- That’s It. You’ll then end up with all the predefined structures which RASA would have built and hence you have now initial supporting files in your directory installed.
Since We have the folder ready , let me explain you about the files, which are created as Initial project structure of Rasa.We are going to implement our logic and codes in these files itself:
__init__.pyan empty file that helps python find your actions which you are going to implement in the future.
actions.pycode for your custom action implementation. In-case you want Rasa to call an external server via REST API or API call, you can define your Custom Actions here. Remember you can create multiple Python Script for Rasa Custom Action.Also, you don't need to train your model again and again, if you have updated this file, you just need to re-run the terminal!
config.yml ‘*’configuration of your NLU and Core models. In-case you are dealing with Tensorflow or Spacy, you need to define such a pipeline here. You Can Also Define other external pipelines, which are mentioned in the documentation of RASA.
credentials.ymldetails for connecting to other services. In case you want to build a Bot on Facebook Messenger, Microsoft Bot Framework, you can maintain such credentials and token here. So basically you just need to add Facebook, slack, and Bot framework related configuration, rasa will automatically do the rest for you. Remember that you need to host Rasa over the https domain. During development, you can use ngrok as a testing tool.
data/nlu.yml ‘*’your NLU training data. Here you can define Intents. Like Order Pizza or Book Uber. You need to add related Sentences for that Intent. Remember if you are using Rasa-X, your training Intent and Data will be added automatically.It's worthwhile to note that , if more amount of training data, more easily your chatbot would be able to extract and recognize intents and entities in a given input by the user to work efficiently
data/stories.yml ‘*’your stories. This is required for Rasa Core. There is something called “Dialog Flow in Rasa” where Rasa Core controls the flow of the conversation between you and chatbot, so for that flow, you need to train chatbot using these stories. So in case you want your chatbot to be very perfect in different contexts (stories) you can add those stories here. Just like Every other ordinary ‘story’, you are the ‘author’ or in this case, ‘developer’- you can define which action needs to be taken against which intent.
domain.yml ‘*’your assistant’s domain. This file combines Different Intent which chatbot can detect and a list of Bot replies. Remember you can define your Custom Action Server Python method name here (in underscore format), so that Rasa will call that python method for you.
endpoints.ymldetails for connecting to channels like FB messenger. This is mainly used for production setup. You can configure your Database external databases so that Rasa can store tracking information.
models/<timestamp>.tar.gzyour initial model.The name of every model will be saved as a timestamp format.
Source Code:
I am going to make a very basic conversational chatbot COVID19 STATS bot. This bot will have the ability to give us information regarding COVID-19-What is covid-19, its basic myth, whether it can spread through food or not, can warm weather prevent it or not. Also, It will tell us the Statistics of a particular area, which is, number of cases reported (Recovered, Active, Deaths)in last 24 hours in a certain state in India.
- First, Let’s Write The Intents and Entities Part in NLU. They are nothing but sample conversation text lines, which will be used to train our RASA model . These Are generally written in
data/nlu.yml file.
So, add these Following Lines to the nlu.yml file:
version: "2.0"nlu:- intent: greetexamples: |- hey- hello- hi- hello there- good morning- good evening- moin- hey there- let's go- hey dude- goodmorning- goodevening- good afternoon- intent: goodbyeexamples: |- good afternoon- cu- good by- cee you later- good night- bye- goodbye- have a nice day- see you around- bye bye- see you later- intent: affirmexamples: |- yes- y- indeed- of course- that sounds good- correct- intent: denyexamples: |- no- n- never- I don't think so- don't like that- no way- intent: mood_greatexamples: |- perfect- great- amazing- feeling like a king- wonderful- I am feeling very good- I am great- I am amazing- I am going to save the world- super stoked- extremely good- so so perfect- so good- so perfect- intent: mood_unhappyexamples: |- my day was horrible- I am sad- I don't feel very well- I am disappointed- super sad- I'm so sad- sad- very sad- unhappy- not good- not very good- extremly sad- so saad- so sad- intent: bot_challengeexamples: |- are you a bot?- are you a human?- am I talking to a bot?- am I talking to a human?- intent: corona_introexamples: |- what is corona?- what is covid-19?- what is novel corona virus?- tell me about corona- can you tell me about corona- can you tell me about covid-19- intent: corona_spreadexamples: |- how does corona virus spread?- how does this virus spread- intent: corona_food_spreadexamples: |- how does food spread corona virus?- does consuming junk food spread corona virus?- how will corona spread from food?- intent: corona_spread_warmexamples: |- how will warm weather stop corona virus?- Can warm weather actually stop corona?- will it stop with warm weather?- intent: corona_spread_riskexamples: |- who is at higher risk of infection- who is more prone to get corona virus infection- intent: corona_communityexamples: |- what is community spread?- how does community spread occurs?- community spread- intent: corona_symptomsexamples: |- What are the symptoms of novel coronavirus?- What are the symptoms of novel corona virus?- What are the symptoms of novel covid-19 virus?- What are the symptoms of corona virus?- symptoms of corona virus?- symptoms of covid19 virus?- intent: corona_mosq_spreadexamples: |- can mosquitoes spread corona virus?- how does mosquitoes spread corona virus?- mosquitoes spread covid-19 virus.- mosquitoes spread covid virus.- intent: corona_vaccineexamples: |- Are there any vaccines available for prevention of corona virus?- Are there any vaccines available for prevention of covid19 virus?- what about vaccines available for prevention of corona virus?- vaccines available for prevention of corona virus?- Have the vaccines been developed for corona virus?- is there any vaccine for corona virus?- is there any vaccine for covid19 virus?- intent: corona_preventexamples: |- how can i prevent covid19?- how can i prevent corona virus?- how can i prevent covid-19?- prevent covid19?- prevent corona- how once can prevent himself/herself from getting infected from covid19?- how once can prevent himself/herself from getting infected from corona?- intent: corona_infected personexamples: |- If You or Someone You Know is Sick or Had Contact with Someone who Has COVID-19- If You or Someone You Know is Sick or Had Contact with Someone who Has corona?- what should i do if someone in my house gets infected by corona?- what should i do if someone in my house gets infected by covid-19?- someone gets covid-19 i know?- my family member gets infected by corona?- someone gets infected by corona i know?- someone gets infected by corona i was in contact with?- someone gets infected by covid i was in contact with?- what to do if someone gets infected by covid i was in contact with?- intent: corona_stats_checkexamples: |- Corona stats- i want to check corona statistics- corona tracker- corona stats- covid tracker- covid stats- intent: corona_states_statexamples: |- [India](state)- I live in [India](state:India)- [Gujarat](state)- I live in [Gujarat](state:Gujarat)- [Andhra Pradesh](state)- I live in [Andhra Pradesh](state:Andhra Pradesh)- [Arunachal Pradesh](state)- I live in [Arunachal Pradesh](state:Arunachal Pradesh)- [Assam](state)- I live in [Assam](state:Assam)- [Bihar](state)- I live in [Bihar](state:Bihar)- [Goa](state)- I live in [Goa](state:Goa)- [Chhattisgarh](state)- I live in [Chhattisgarh](state:Chhattisgarh)- [Haryana](state)- I live in [Haryana](state:Haryana)- [Himachal Pradesh](state)- I live in [Himachal Pradesh](state:Himachal Pradesh)- [Jharkhand](state)- [Karnataka](state)- [Madhya Pradesh](state)- [Kerala](state)- [Maharashtra](state)- [Manipur](state)- [Meghalaya](state)- [Mizoram](state)- [Nagaland](state)- [Odisha](state)- [Punjab](state)- [Rajasthan](state)- [Sikkim](state)- [Tamil Nadu](state)- [Telangana](state)- [Tripura](state)- [Uttar Pradesh](state)- [Uttarakhand](state)- [West Bengal](state)- intent: name_entryexamples: |- My name is [Ashish](name)- [ashish](name:Ashish) this side- It's [ashish](name:Ashish)- [ashish](name:Ashish) is my name- [Ashish](name)- [Rahul](name)- [ashish](name:Ashish)- My name is [Harsh](name)- [harsh](name:Harsh) this side- It's [harsh](name:Harsh)- [harsh](name:Harsh) is my name- [Harsh](name)- [Rahul](name)- [harsh](name:Harsh)- It is [Tom](name)- My name is [Bob](name)- my name is [Harsh](name)- name is [Rajesh](name)- my name is [demo](name)- name is [demo](name)- I am [Harsh](name)- I am [Nandan](name)- I am [demo](name)- myself [Harsh](name)- myself [demo](name)
Here Entities are being enclosed in square brackets and we are giving them a common class so that our model can understand the sample texts and respond accordingly
For example: Given Below, Uttrakhand and West Bengal are Entities [square brackets]and we have given them a common class (state) in round brackets.
- [Uttarakhand](state)
- [West Bengal](state)As you can see , we are defining all possible questions a human can ask to our bot . There is no limit in these intent examples; the more intent examples, the better our chatbot will be able to perform or recognize the response to a particular question asked by the user!
2.Next , we will define our stories. Stories are organized blocks of intent and actions, following a specific path.So what does that mean?
Well, Stories are a way to link intents and their respective actions. Actions are the responses that will be made by our bot in response to a particular text entered by the user. We have the ability to train our bot by feeding it with sample conversations in the format of intents and actions respectively.
Write the following lines in data/stories.yml file :
For Example , in the story happy path , when we will say suppose hey or hi ,our bot will respond in the action of utter greet. This utter greet action has a sample text as -” Hi . I am a bot, how can I help you?”.Then if we replied in mood_great intent, like for example, I am good or I am feeling good, out bot will reply in accordance with utter happy action. It will follow a certain trail or path, which we define in stories eventually.
version: "2.0"stories:- story: happy pathsteps:- intent: greet- action: utter_greet- intent: mood_great- action: utter_happy- story: sad path 1steps:- intent: greet- action: utter_greet- intent: mood_unhappy- action: utter_cheer_up- action: utter_did_that_help- intent: affirm- action: utter_happy- story: sad path 2steps:- intent: greet- action: utter_greet- intent: mood_unhappy- action: utter_cheer_up- action: utter_did_that_help- intent: deny- action: utter_goodbye- story: corona introsteps:- intent: corona_intro- action: utter_corona_intro- story: corona spreadsteps:- intent: corona_spread- action: utter_corona_spread- story: corona food spreadsteps:- intent: corona_food_spread- action: utter_corona_food_spread- story: corona warm weathersteps:- intent: corona_spread_warm- action: utter_corona_spread_warm- story: corona risksteps:- intent: corona_spread_risk- action: utter_corona_spread_risk- story: corona tracker pathsteps:- intent: greet- action: utter_greet- intent: corona_stats_check- action: utter_corona_stats_check- intent: corona_states_stat- action: actions_corona_state_stat- story: corona tracker 2 pathsteps:- intent: corona_stats_check- action: utter_corona_stats_check- intent: corona_states_stat- action: actions_corona_state_stat- story: corona community spread pathsteps:- intent: corona_community- action: utter_corona_community- story: corona mosquito pathsteps:- intent: corona_mosq_spread- action: utter_corona_mosq_spread- story: corona prevent pathsteps:- intent: corona_prevent- action: utter_corona_prevent- story: corona symptoms pathsteps:- intent: corona_symptoms- action: utter_corona_symptoms- story: corona vaccine pathsteps:- intent: corona_vaccine- action: utter_corona_vaccine- story: corona infected pathsteps:- intent: corona_infected person- action: utter_corona_infected person
We Can write various stories, that will purely depend on you and your respective domain knowledge; what type or theme you have decided to make a bot for!
It is important to note here that you have to design the path in a very specific way , if you have insufficient training data, it might lead to a wrong or undesired response from the bot.
3.Next, we have Rules. Rules are a type of training data used to train your assistant’s dialogue management model. Rules describe short pieces of conversations that should always follow the same path. Rules generally put some constraints or restrictions to follow a certain path, which makes it different from stories, where you can follow any path with any sort of intent and action you want to customize!
We define them on data/rules.yml
version: "2.0"
rules:
- rule: Say goodbye anytime the user says goodbye
steps:
- intent: goodbye
- action: utter_goodbye
- rule: Say 'I am a bot' anytime the user challenges
steps:
- intent: bot_challenge
- action: utter_iamabot4.Then, we have Configurations. This file is used to maintain or edit the current configuration of our bot’s model. The configuration file defines the NLU and Core components that your model will use. The language and pipeline keys specify how the NLU model should be built. You can learn about choosing components for your NLU pipeline here. The policies key defines the policies that the Core model will use.
We define them in config.ymlfile :
# Configuration for Rasa NLU.
# https://rasa.com/docs/rasa/nlu/components/
language: en
pipeline:
# # No configuration for the NLU pipeline was provided. The following default pipeline was used to train your model.
# # If you'd like to customize it, uncomment and adjust the pipeline.
# # See https://rasa.com/docs/rasa/tuning-your-model for more information.
# - name: WhitespaceTokenizer
# - name: RegexFeaturizer
# - name: LexicalSyntacticFeaturizer
# - name: CountVectorsFeaturizer
# - name: CountVectorsFeaturizer
# analyzer: char_wb
# min_ngram: 1
# max_ngram: 4
# - name: DIETClassifier
# epochs: 100
# - name: EntitySynonymMapper
# - name: ResponseSelector
# epochs: 100
# Configuration for Rasa Core.
# https://rasa.com/docs/rasa/core/policies/
policies:
# # No configuration for policies was provided. The following default policies were used to train your model.
# # If you'd like to customize them, uncomment and adjust the policies.
# # See https://rasa.com/docs/rasa/policies for more information.
# - name: MemoizationPolicy
# - name: TEDPolicy
# max_history: 5
# epochs: 100
# - name: RulePolicyIf you can see here , most of the lines are being commented. The reason is we only uncomment these lines , when we need them to update. if we put them in commented format , our core model will use the default pipelines and policies , on which it was initially trained on.
You Can Check out the official documentation of RASA for various available policies and pipelines. The main advantage of RASA here is that it provides flexibility and makes it easy to work with multiple types of supported pipelines and policies available to handle operations of text type of data.
5.Next We will look into Actions. Actions is something which we can manipulate or define in a custom fashion- we can make our own actions , logical actions, which the bot will reply in response to user’s input.After each user message, the model will predict an action that the assistant should perform next.A custom action is an action that can run any code you want. This can be used to make an API call or to query a database for example.
We define actions in actions/actions.py file:
# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/custom-actions
# This is a simple example for a custom action which utters "Hello World!"
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
import requests
# class ActionHelloWorld(Action):
#
# def name(self) -> Text:
# return "action_hello_world"
#
# def run(self, dispatcher: CollectingDispatcher,
# tracker: Tracker,
# domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
#
# dispatcher.utter_message(text="Hello World!")
#
# return []
class ActionHelloLoc(Action):
def name(self) -> Text:
return "action_get_loc"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
slot_name = tracker.get_slot("state")
print("slotname", slot_name)
dispatcher.utter_message(
text="So You Live In " + slot_name.title() + " , Here Are Your Location's Corona Stats: \n")
return []
class Actioncoronastats(Action):
def name(self) -> Text:
return "actions_corona_state_stat"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
responses = requests.get("https://api.covid19india.org/data.json").json()
entities = tracker.latest_message['entities']
print("Now Showing Data For:", entities)
state = None
for i in entities:
if i["entity"] == "state":
state = i["value"]
message = "Please Enter Correct State Name !"
if state == "india":
state = "Total"
for data in responses["statewise"]:
if data["state"] == state.title():
print(data)
message = "Now Showing Cases For --> " + state.title() + " Since Last 24 Hours : "+ "\n" + "Active: " + data[
"active"] + " \n" + "Confirmed: " + data["confirmed"] + " \n" + "Recovered: " + data[
"recovered"] + " \n" + "Deaths: " + data["deaths"] + " \n" + "As Per Data On: " + data[
"lastupdatedtime"]
print(message)
dispatcher.utter_message(message)
return []I have written a few custom actions, in which I have tried to integrate the API link of covid19 tracker to my chatbot in .json format. These API calls would help us get the latest data of COVID-19 for the last 24 hours and will show the following Cases:
- Recovered Cases
- Active Cases
- Deaths
You can use this API :
API:"https://api.covid19india.org/data.json"
Source: https://www.covid19india.org/NOTE: If you implement some custom actions in actions.yml , you need to edit or uncomment some code in endpoints.yml . Else, you won't be able to debug or run your custom actions with your chatbot.
# This file contains the different endpoints your bot can use.
# Server where the models are pulled from.
# https://rasa.com/docs/rasa/model-storage#fetching-models-from-a-server
#models:
# url: http://my-server.com/models/default_core@latest
# wait_time_between_pulls: 10 # [optional](default: 100)
# Server which runs your custom actions.
# https://rasa.com/docs/rasa/custom-actions
action_endpoint:
url: "http://localhost:5055/webhook"
# Tracker store which is used to store the conversations.
# By default the conversations are stored in memory.
# https://rasa.com/docs/rasa/tracker-stores
#tracker_store:
# type: redis
# url: <host of the redis instance, e.g. localhost>
# port: <port of your redis instance, usually 6379>
# db: <number of your database within redis, e.g. 0>
# password: <password used for authentication>
# use_ssl: <whether or not the communication is encrypted, default false>
#tracker_store:
# type: mongod
# url: <url to your mongo instance, e.g. mongodb://localhost:27017>
# db: <name of the db within your mongo instance, e.g. rasa>
# username: <username used for authentication>
# password: <password used for authentication>
# Event broker which all conversation events should be streamed to.
# https://rasa.com/docs/rasa/event-brokers
#event_broker:
# url: localhost
# username: username
# password: password
# queue: queue- You Need to make sure this line is uncommented to run custom actions:
action_endpoint:
url: "http://localhost:5055/webhook"6.Finally, we will define our Domain File. The Domain defines the universe in which your assistant operates. It specifies the intents, entities, slots, responses and actions your bot should know about. It also defines a session_config to configure conversation sessions.
We generally define the domain content in domain.ymlfile :
version: "2.0"intents:- greet- goodbye- affirm- deny- mood_great- mood_unhappy- bot_challenge- corona_intro- corona_spread- corona_food_spread- corona_spread_warm- corona_spread_risk- corona_states_stat- name_entry- location_entry- corona_stats_check- corona_community- corona_mosq_spread- corona_prevent- corona_symptoms- corona_vaccine- corona_infected personactions:- actions_corona_state_stat- actions_name_show- action_hello_world_action_test- action_get_locentities:- name- stateslots:name:type: unfeaturizedstate:type: unfeaturizedresponses:utter_greet:- text: "Hey! Welcome To COVID-19 Stats Bot.\nYou Can Ask Me about Following Info:\n1.To Check Corona Statistics Of Your State Where You Live , Type 'Corona Tracker' or 'corona stats'\n2.What Is COVID-19?\n3.How does Corona Spread?\n4.How does food spread corona virus?\n5.Can warm weather actually stop corona?\n6.Who is more prone to get corona virus infection?\n7.About Community Spread from COVID-19\n8.Preventive Steps to be taken for COVID-19\n9.COVID-19 Symptoms Check\n10.About Vaccine Availability For COVID-19\n11.How To Handle Someone who gets in contact with infected person from COVID-19."utter_cheer_up:- text: "Here is something to cheer you up:"image: "https://i.imgur.com/nGF1K8f.jpg"utter_did_that_help:- text: "Did that help you?"utter_happy:- text: "Great, carry on!"utter_goodbye:- text: "GoodBye! Hope To See You Soon :))"utter_iamabot:- text: "I am a COVID-19 Stats bot, Developed By Harsh Sharma.\nCheck Out My Github Profile For More Amazing Projects: 'https://github.com/harshgeek4coder' \nCheck Out My Website: 'http://theharshsharma.tech/'\n"utter_corona_intro:- text: "The COVID-19 pandemic, also known as the coronavirus pandemic, is an ongoing pandemic of coronavirus disease 2019 (COVID-19) caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2).[1] The disease was first identified in December 2019 in Wuhan, China.[4] The World Health Organization declared the outbreak a Public Health Emergency of International Concern on 30 January 2020 and a pandemic on 11 March 2020. As of 24 September 2020, more than 31.9 million cases have been reported in 188 countries and territories, resulting in more than 978,000 deaths; more than 22 million people have recovered."utter_corona_spread:- text: "This virus was first detected in Wuhan City, Hubei Province, China. The\\ first infections were linked to a live animal market, but the virus is now\\ spreading from person-to-person. It’s important to note that person-to-person\\ spread can happen on a continuum. Some viruses are highly contagious (like\\ measles), while other viruses are less so. The virus that causes COVID-19\\ is spreading from person-to-person. Someone who is actively sick with COVID-19\\ can spread the illness to others. That is why we recommend that these patients\\ be isolated either in the hospital or at home (depending on how sick they\\ are) until they are better and no longer pose a risk of infecting others.\n\How long someone is actively sick can vary so the decision on when to release\\ someone from isolation is made on a case-by-case basis in consultation with\\ doctors, infection prevention and control experts, and public health officials\\ and involves considering specifics of each situation including disease severity,\\ illness signs and symptoms, and results of laboratory testing for that patient.\n\The virus that causes COVID-19 seems to be spreading easily and sustainably\\ in the community (“community spread”) in some affected geographic areas. Community\\ spread means people have been infected with the virus in an area, including\\ some who are not sure how or where they became infected."utter_corona_food_spread:- text: "It is highly unlikely that people can contract COVID-19 from food or food packaging. COVID-19 is a respiratory illness and the primary transmission route is through person-to- person contact and through direct contact with respiratory droplets generated when an infected person coughs or sneezes."utter_corona_spread_warm:- text: "With the start of summer coming soon, many are hopeful that the warmer weather will slow the spread of SARS-CoV-2, the novel coronavirus that causes COVID-19. There have been hints from lab experiments that increased temperature and humidity may reduce the viability of SARS-CoV-2. Meanwhile, other coronaviruses that cause less severe diseases, such as the common cold, do spread more slowly among people during the summer."utter_corona_spread_risk:- text: "COVID-19 is often more severe in people who are older than 60 years or who have health conditions like lung or heart disease, diabetes or conditions that affect their immune system.If you’re at high risk, know what to do, and take the right actions now to protect yourself.If you’re not at high risk, do your part to prevent the spread of coronavirus to those who are.There is increasing evidence that people with existing chronic conditions or compromised immune systems due to disability are at higher risk of death due to COVID-19."utter_country_entry:- text: "Can You Please Enter Your Current State From India?"utter_corona_stats_check:- text: "Covid-19 Tracker [India] :\nPlease Enter the State name you wish to see the Covid-19 Statistics : "utter_corona_community:- text: "Community spread means people have been infected with the virus in an area, including some who are not sure how or where they became infected. Each health department determines community spread differently based on local conditions. For information on community spread in your area, please visit your health department’s website."utter_corona_mosq_spread:- text: "At this time, CDC has no data to suggest that this new coronavirus or other similar coronaviruses are spread by mosquitoes or ticks. The main way that COVID-19 spreads is from person to person. See How Coronavirus Spreads for more information."utter_corona_prevent:- text: "The best way to prevent illness is to avoid being exposed to the virus. CDC recommends everyday preventive actions to help prevent the spread of respiratory diseases.CDC recommends washing your hands often with soap and water for at least 20 seconds especially after you have been in a public place, or after blowing your nose, coughing, or sneezing. If soap and water are not available, CDC recommends using an alcohol-based hand sanitizer that contains at least 60 percent alcohol.\nWear masks in public settings when around people not living in your household and particularly where other social distancing measures are difficult to maintain, such as grocery stores, pharmacies, and gas stations. Masks may slow the spread of the virus and help people who may have the virus and do not know it from transmitting it to others.COVID-19 can be spread by people who do not have symptoms and do not know that they are infected. That’s why it’s important for everyone to practice social distancing (staying at least 6 feet away from other people) and wear masks in public settings. Masks provide an extra layer to help prevent the respiratory droplets from traveling in the air and onto other people.The masks recommended are not surgical masks or N-95 respirators. Those are critical supplies that must continue to be reserved for healthcare workers and other medical first responders, as recommended by current CDC guidance."utter_corona_symptoms:- text: "COVID-19 symptoms include:\nCough\nFever or chills\nShortness of breath or \ndifficulty breathing\nMuscle or body aches\nSore throat\nNew loss of taste or smell\nDiarrhea\nHeadache\nNew fatigue\nNausea or vomiting\nCongestion or runny noseIn confirmed cases of illness in humans, common symptoms have been acute, serious respiratory illness with fever, cough, shortness of breath, and breathing difficulties. Based on current clinical experience, the infection generally presents as pneumonia. It has caused kidney failure and death in some cases. It is important to note that the current understanding of the illness caused by this infection is based on a limited number of cases and may change as more information becomes available."utter_corona_vaccine:- text: "Human clinical trials for Covid-19 vaccine initiated in India: ICMR. Human clinical trials for a vaccine for Covid-19 have been initiated with approximately 1,000 volunteers participating in the exercise for each of the two indigenously developed vaccine candidates\nWhile some western, traditional or home remedies may provide comfort and alleviate symptoms of mild COVID-19, there are no medicines that have been shown to prevent or cure the disease. WHO does not recommend self-medication with any medicines, including antibiotics, as a prevention or cure for COVID-19. However, there are several ongoing clinical trials of both western and traditional medicines. WHO is coordinating efforts to develop vaccines and medicines to prevent and treat COVID-19 and will continue to provide updated information\nAt this time there is no vaccine to prevent coronavirus disease 2019 (COVID-19). The FDA is working with vaccine developers and other researchers and manufacturers to help expedite the development and availability of medical products such as vaccines, antibodies, and drugs to prevent COVID-19"utter_corona_infected person:- text: "Stay home for 14 days after your last contact with a person who has COVID-19.\nBe alert for symptoms. Watch for fever, cough, shortness of breath, or other symptoms of COVID-19.\nIf possible, stay away from others, especially people who are at higher risk for getting very sick from COVID-19."session_config:session_expiration_time: 60carry_over_slots_to_new_session: true
Here, we have defined all our intents, entities, actions, slots, and forms. Also here, we define our sample responses to train our bot. These sample conversations starts with utter a keyword, and also have a specified format placed in front of them, like text , image ,category,boolean,etc.
We can call Domain as the entire sample space or universe for our bot , since this space defines all the links , intents , entities , custom actions and everything in it and helps to train the model accordingly.
Enough With Writing Codes, let’s train it up!
Training Your Bot:
- Once You Are Done with writing your code in all of the required files in your Bot’s Directory, its time to train our model.
- You Need to Start a terminal, you can use windows command prompt terminal or you can use an inbuilt terminal in IDE
- If you are using Pycharm, you can open a terminal, by right-clicking on the main folder directory; Simple Open a terminal in your IDE , and write the following Code:
rasa train
- If you have written the code in the proper format, without any errors, then you would be able to train your model successfully, without any errors, and you will get something like this:
NOTE: If you get any errors, try to diagnose it through; check the relevant files logged in logs with error messages
Once We are done with training, we can test our chatbot by running it!
Running Chatbot Using Command Terminal :
- You Need to Start a terminal, you can use windows command prompt terminal or you can use an inbuilt terminal in IDE
- If you are using Pycharm, you can open a terminal, by right-clicking on the main folder directory :
- Then Type the Following to Activate our Chatbot:
rasa shell
Also, We need to run our actions file to, but in a different terminal. You can either open a command prompt using start, or you can open another local terminal adjacent to the rasa shell terminal.
Type The following in a new terminal : rasa run actions
- Run both of the terminals and wait for it to load up.
- If you have trained your model successfully without any errors, you would be able to run the bot successfully, and you will get something like this:
- After the Bot Runs Successfully, you would be able to Enter your Responses and you should get appropriate predicted responses too!
Demonstration of how to launch chatbot in local terminal:
Testing And Deployment:
- Since we have tested our chatbot on the local terminal, its time to deploy our chatbot online, so that anyone can use it easily
- Rasa offers a wide range of options to deploy our chatbot very easily using services like Ngrok and Docker.
- Some popular choices of developers for deploying RASA chatbot are:
- Telegram
- Slack
- Your Own Website
- Facebook Messenger
- Twilio
- Google Hangouts Chat
- Microsoft Bot Framework
- Cisco Webex Teams
- RocketChat
- Mattermost
NOTE: I am going to explain how to deploy RASA chatbot temporarily; since for permanent deployment,Actions.pyneeds to be run on a docker container as a backend server, which could lead to very complex coding! Therefore, for the sake of simplicity, I am gonna explain how to deploy a rasa chatbot without the Docker’s Backend Implementation.
Let us deploy our chatbot on telegram:
- Download ngrok from https://ngrok.com/download
- After extracting the zip file, open the ngrok file and run it.
- In ngrok, enter the command ‘ngrok http 5005 ’:
Next , we have to get access to telegram:
- Go to telegram and search for Botfather.Then create your own bot using Botfather:
a) Open the telegram app and search for botfather(it is an inbuilt bot which can be used to create other bots)
b) Start a conversation with botfather and enter : /newbot to create a newbot.
c) Give a name to your bot
d) Give a username to your bot, which must end in _bot.This generates an access token.
Here, for example, I have given the following details:
- Name: Covid19_FAQ_hs_Bot
- Username of the Bot:covid_19_faq_hs_bot
- After entering these details, you will get a unique API token key.
- Then Go to your project directory and search and open credentials.yml.
- In that file enter the following code:
telegram:access_token: "obtained from telegram"verify: "your bot username"webhook_url: "https://<ngrokurl>/webhooks/telegram/webhook"
· Go to the terminal and enter the command ‘rasa run’
· Open one more terminal and run the command ‘rasa run actions’
If you have followed the steps correctly, you will be able to test the deployed chatbot very easily.
Now, you can chat with your bot from Telegram. Just Search for your bot with the name you entered as Name of the Bot.
Congratulations, Your Chatbot is live now! You can start using it by entering ‘Hey’ in it.
Demonstration of how to deploy a chatbot and integrate with a telegram:
Pros :
- Easy To Understand and Implement
- Chatbots Can be easily deployed or integrated over a wide range of channels.
- Custom Actions Can be implemented very easily, which enables a developer to code desirable operations from a chatbot.
- Supports a wide range of external policies and other open-source pipelines and frameworks like spacy, duckling, etc
- Provides Interactive ways of deploying and testing, with single-lined commands.
Cons :
- It might take some time to train your model, which is a bit annoying.
- If the proper format or indentation is not followed, it won't train the whole model. You have to manually evaluate the errors in every single line of model files.
- You Need to have a proper environment set up with all prerequisites and appropriate pipelines and packages installed in that specific environment. Else, it won't support the Extraction of entities and intents properly.
- You Need to have a certain knowledge of Dockers and Kubernetes in order to deploy your chatbot online permanently.
Conclusion:
So Far, I tried my very best to explain to you the RASA Framework-From Intuition to Deployment. Being an NLP Enthusiast, developing a chatbot with RASA really intrigued me; I must say, it was an amazing experience to work with RASA. Being an open-source Framework and its flexibility to support external pipelines and packages, it makes it easy for a developer to add custom stories, intents, paths, entities, sample conversations, custom logical responses, and actions to get a robust and deployable chatbot. Also, being an open-source framework, RASA keeps getting updated accordingly after getting inputs from developers on their community forums, which makes it more favorable for developers to test and enhance the development of chatbots more conveniently.
Its inbuilt features make it easy to pre-process text, no matter how long it is or needs to be cleaned, and remove all sorts of stop words and punctuation. We don't need to explicitly define additional layers or update internal model architecture; just with few simple lines of code, our model will start training, and just in a few minutes, our model will be trained and ready to test out. Also, It saves and keeps a track of all models trained in a separate directory, in case you wish to roll out or get back to the previous state of the model while testing or updating the chatbot, or in case it gets some errors in it, which makes it even more manageable for developers to keep track of their development progress without losing any previous information!
Custom Actions makes our work easier; since we can use it to get our desired operations in a more fashionable way, we can use custom actions to integrate it with API and JSON in order to link our chatbot with databases and thus, can help to fetch data for users in any part of the world in no time, thus maintaining customer engagement on behalf of industries and organizations.
In the end, I would say for all the NLP Enthusiasts, it is the best source to start working and getting your way around developing the chatbots!
Source Code:
You Can Checkout This Project’s Source Code Here: RASA Chatbot Project Source Code